//
//  GetPhotoTools.m
//  PianoAndPoemDirectSeeding
//
//  Created by 梅津山 on 2017/7/10.
//  Copyright © 2017年 梅津山. All rights reserved.
//

#import "GetPhotoTools.h"



@interface GetPhotoTools ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, strong) UIImagePickerController *pickerVc;
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation GetPhotoTools

+ (instancetype)getPhotoShared{
    static GetPhotoTools *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[GetPhotoTools alloc] init];
    });
    return instance;
}

- (void)alterGetPhoto:(UIViewController *)vc {
    _pickerVc = [[UIImagePickerController alloc] init];
    //设置代理
    _pickerVc.delegate = self;
    //设置弹窗
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"图片来源" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //添加功能按钮
    //相册
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //相册获取图片的方法
        [self getImageWithAlbum:vc];
    }];
    
    [alertVc addAction:albumAction];
    
    //相机
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //相机获取图片的方法
        [self getImageWithCamera:vc];
    }];
    [alertVc addAction:cameraAction];
    
    //取消
    UIAlertAction *cacaelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVc addAction:cacaelAction];

    [vc presentViewController:alertVc animated:YES completion:nil];
}

//相册
- (void)getImageWithAlbum:(UIViewController *)vc{
    //判断当前设备是否支持当前图片来源
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        //设置当前来源
        self.pickerVc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //设置选择图片时是否编辑
        //        self.pickerVc.allowsEditing = YES;
        //显示图片选择控制器视图
        [vc presentViewController:self.pickerVc animated:YES completion:nil];
    }
}

- (void)getImageWithCamera:(UIViewController *)vc {
    //判断当前设备是否支持当前图片来源
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //设置当前来源
        self.pickerVc.sourceType = UIImagePickerControllerSourceTypeCamera;
        //设置选择图片时是否编辑
        //        self.pickerVc.allowsEditing = YES;
        //显示图片选择控制器视图
        [vc presentViewController:self.pickerVc animated:YES completion:nil];
    }else {
        NSLog(@"你的手机没有摄像功能");
    }
}


#pragma mark - UIImagePickerControllerDelegate代理方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"取消了相册选择控制器");
    //需要手动设置取消功能
    [self.pickerVc dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //首先应该先退出当前照片选择页面
    [self.pickerVc dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"%@",info);
    //获得原始图片
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];
    //获得编辑后的图片
    //    UIImage *editImage = info[@"UIImagePickerControllerEditedImage"];
    //将获得的图片设置给视图
//    _imageView.image = image;
    if (self.imageBlock) {
        self.imageBlock(image);
    }
    
    //如果需要保存当前的图片
//    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

////  图片保存的回调方法
////这个方法必须严格按照系统要求的格式来写
//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
//    
//}



@end
